package com.spiderman.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.spiderman.entity.Movie;
import com.spiderman.entity.TianMaoListItem;

/**
 * Description: 爬虫正则表达式工具类
 * @author Kwum
 * @date 2017年8月5日 上午11:18:06
 * @version 1.0
 */

public class RegExUtil{
    
    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
//    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    private static final String regEx_space = "\\s*|\t|\r|\n";// 定义空格回车换行符
//    private static final String regEx_w = "<w[^>]*?>[\\s\\S]*?<\\/w[^>]*?>";//定义所有w标签
    
    //例子
    private static final String temp = "<dlclass=\"item\"\"data-id=\"43711318351\">\n<dtclass=\"photo\">\n<aclass=\"J_TGoldData\"atpanel=\"1,43711318351,50050691,-43711318351,shopsearch,1,shopcon,2240645040,,\"href=\"//detail.tmall.com/item.htm?id=43711318351&amp;rn=ec130b92f05a5ff7d1203080bb5772b2&amp;abbucket=0\"target=\"_blank\"data-gold-url=\"/inshopse\"data-gold-data=\"{gokey:at_bucketid=inshop_b_alg:7960;&amp;srppage=1&amp;scid=&amp;lf_aclog=1-43711318351-60-null-2240645040&amp;?src=shopsystem--11.227.0.150&amp;sort=default&amp;ss_bucket=0&amp;rank_src=inshop_pc_tm&amp;shop_id=113421539&amp;navigator=all&amp;stat=sku_or,type:cnt,count:1000&amp;s=0&amp;n=60&amp;app=inshop&amp;outfmt=json&amp;bts=%7B%22inshop_b_alg%22%3A%7B%22bucket%22%3A%7B%22name%22%3A%222%22%2C%22id%22%3A7960%2C%22groups%22%3A%7B%22inshop_b%22%3A%22%22%2C%22qp4inshop%22%3A%22%22%2C%22qp4mall%22%3A%22s%3Dmall_inshop_pc%22%2C%22qp4tmall%22%3A%22a%3Db%22%2C%22qpparams%22%3A%22s%3Dmall_inshop_pc%22%2C%22qrs4inshop%22%3A%22%22%2C%22sp%22%3A%22qp%3Ds%3Amall%22%7D%7D%7D%7D&amp;stats_click=&amp;rn=117a90955ccb2332672e6b61a56c9bbe,cna:,bc_type:b}\"><imgalt=\"锦福里喜饼喜蛋礼盒诞生礼盒创意百日宴宝宝周岁满月回礼喜糖礼盒\"data-ks-lazyload=\"//img.alicdn.com/bao/uploaded/i3/TB196_SLXXXXXbKXpXXXXXXXXXX_!!0-item_pic.jpg_180x180.jpg\"src=\"//assets.alicdn.com/s.gif\"></a>\n</dt>\n<!--item.discntPrice:10.58-->\n<ddclass=\"detail\">\n<aclass=\"item-name\"j_tgolddata\"atpanel=\"1,43711318351,50050691,,shopsearch,1,shopcon,2240645040,,\"href=\"//detail.tmall.com/item.htm?id=43711318351&amp;rn=ec130b92f05a5ff7d1203080bb5772b2&amp;abbucket=0\"target=\"_blank\"data-gold-url=\"/inshopse\"data-gold-data=\"{gokey:at_bucketid=inshop_b_alg:7960;&amp;srppage=1&amp;scid=&amp;lf_aclog=1-43711318351-60-null-2240645040&amp;?src=shopsystem--11.227.0.150&amp;sort=default&amp;ss_bucket=0&amp;rank_src=inshop_pc_tm&amp;shop_id=113421539&amp;navigator=all&amp;stat=sku_or,type:cnt,count:1000&amp;s=0&amp;n=60&amp;app=inshop&amp;outfmt=json&amp;bts=%7B%22inshop_b_alg%22%3A%7B%22bucket%22%3A%7B%22name%22%3A%222%22%2C%22id%22%3A7960%2C%22groups%22%3A%7B%22inshop_b%22%3A%22%22%2C%22qp4inshop%22%3A%22%22%2C%22qp4mall%22%3A%22s%3Dmall_inshop_pc%22%2C%22qp4tmall%22%3A%22a%3Db%22%2C%22qpparams%22%3A%22s%3Dmall_inshop_pc%22%2C%22qrs4inshop%22%3A%22%22%2C%22sp%22%3A%22qp%3Ds%3Amall%22%7D%7D%7D%7D&amp;stats_click=&amp;rn=117a90955ccb2332672e6b61a56c9bbe,cna:,bc_type:b}\">锦福里喜饼喜蛋礼盒诞生礼盒创意百日宴宝宝周岁满月回礼喜糖礼盒</a>\n<divclass=\"attribute\">\n<divclass=\"cprice-area\">\n<spanclass=\"symbol\">&yen;</span>\n<spanclass=\"c-price\">10.58</span>\n</div>\n<divclass=\"sale-area\">\n总销量：\n<spanclass=\"sale-num\">142778</span>\n</div>\n</div>\n</dd>\n<ddclass=\"rates\">\n<divclass=\"title\">\n<h4>|<ahref=\"//detail.tmall.com/item.htm?id=43711318351&amp;rn=ec130b92f05a5ff7d1203080bb5772b2&amp;abbucket=0&amp;on_comment=1#J_TabBar\"target=\"_blank\"><span>评价:1351</span></a></h4>\n</div>\n</dd>\n</dl>";
    //自定义正则表达式，需要找的一段数据先从html中复制出来，变量改成(.*?)，"改成\"
    private static final String regEx_tianmao = "<dlclass=\"item\"\"data-id=\"(.*?)\">"
            + "\n<dtclass=\"photo\">"
            + "\n<aclass=(.*?)></a>"
            + "\n</dt>"
            + "\n<!--item.discntPrice:(.*?)-->"
            + "\n<ddclass=\"detail\">"
            + "\n<aclass=(.*?)>(.*?)</a>"
            + "\n<divclass=\"attribute\">"
            + "\n<divclass=\"cprice-area\">"
            + "\n<spanclass=\"symbol\">&yen;</span>"
            + "\n<spanclass=\"c-price\">(.*?)</span>"
            + "\n</div>"
            + "\n<divclass=\"sale-area\">"
            + "\n总销量："
            + "\n<spanclass=\"sale-num\">(.*?)</span>"
            + "\n</div>"
            + "\n</div>"
            + "\n</dd>"
            + "\n<ddclass=\"rates\">"
            + "\n<divclass=\"title\">"
            + "\n<h4>(.*?)><span>评价:(.*?)</span></a></h4>"
            + "\n</div>"
            + "\n</dd>"
            + "\n</dl>";
    
    /**
     * 初始化过滤
     * @param str 页面内容
     * @author kwum
     */
    public static void init(String str){
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(str);
        str = m_script.replaceAll(""); // 过滤script标签
 
        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(str);
        str = m_style.replaceAll(""); // 过滤style标签
 
        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(str);
        str = m_space.replaceAll(""); // 过滤空格回车标签
    }
    
    /**
     * 获取自定义正则表达式里面的内容（电影）
     * @param str 熟肉
     * @param regEx 自定义正则
     * @return 电影列表
     * @author kwum
     */
    public static List<Movie> getMovie(String str, String regEx) {
        init(str);
        
        List<Movie> list = new ArrayList<>();
        Pattern p = Pattern.compile(regEx);//自定义匹配
        str = str.replace(" ", "");
        Matcher macher = p.matcher(str);  
        while(macher.find()){ 
            list.add(new Movie(macher.group(3).trim(), macher.group(7).trim(), macher.group(6).trim(), macher.group(8).trim()));
        }
        return list;
    }
    
    /**
     * 获取自定义正则表达式里面的内容（天猫列表）
     * @param str 熟肉
     * @param regEx 自定义正则
     * @return 天猫列表
     * @author kwum
     */
    public static List<TianMaoListItem> getTianMaoListItem(String str, String regEx) {
        init(str);
        
        List<TianMaoListItem> list = new ArrayList<>();
        
        Pattern p = Pattern.compile(regEx);//自定义匹配
        str = str.replace(" ", "");
        str = str.replace("\\&quot;", ""); // 过滤空格回车标签
        str = str.replace("\\", ""); // 过滤空格回车标签
        
        Matcher macher = p.matcher(str);
        while(macher.find()){ 
            list.add(new TianMaoListItem(macher.group(1), macher.group(5), macher.group(7), macher.group(3), macher.group(9)));
        }
        return list;
    }
    
    public static void main (String[] args){
        Pattern p = Pattern.compile(regEx_tianmao);  
        Matcher macher = p.matcher(temp);
        int i = 1;
        while(macher.find()){
            System.out.println(macher.group(1));
            System.out.println(macher.group(2));
            System.out.println(macher.group(3));
            System.out.println(macher.group(4));
            System.out.println(macher.group(5));
            System.out.println(macher.group(6));
            System.out.println(macher.group(7));
            System.out.println(macher.group(8));
            System.out.println(macher.group(9));
            System.out.println("////////////////////////////////////");
            System.out.println(i++);
       }
    }
}
